<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="上级" prop="parentId">
        <treeselect
          v-model="form.parentId"
          :options="options"
          :disable-branch-nodes="false"
          :show-count="true"
          :normalizer="normalizer"
          :flatten-search-results="false"
          noOptionsText="无上级可选"
          placeholder="请选择上级"
        />
      </el-form-item>
      <el-form-item label="名称" prop="categoryName">
        <el-input v-model.trim="form.categoryName" autocomplete="off" placeholder="请输入类别名称" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="类别标识" prop="categoryCode">
        <el-input v-model.trim="form.categoryCode" autocomplete="off" placeholder="请输入类别编码" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="类服务名" prop="beanName">
        <el-input v-model.trim="form.beanName" autocomplete="off" maxlength="200"></el-input>
      </el-form-item>
      <el-form-item label="类包名" prop="beanClass">
        <el-input v-model.trim="form.beanClass" autocomplete="off" maxlength="200"></el-input>
      </el-form-item>
      <el-form-item label="显示排序" prop="sortNum">
        <el-input-number v-model="form.sortNum" :min="1" :max="1000000" placeholder="数值越小越靠前" style="width: 100%"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="form.remark" type="textarea" maxlength="100"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getList, doAdd, doEdit } from '@/api/workflow/modelCategory'
  import Treeselect from '@riophae/vue-treeselect'
  import { handleTree } from '@/utils/index'

  export default {
    name: 'ModelCategoryEdit',
    components: { Treeselect },
    data() {
      return {
        form: {
          categoryName: undefined,
          categoryCode: undefined,
          parentId: undefined,
          sortNum: 1,
        },
        rules: {
          categoryName: [{ required: true, trigger: ['blur', 'change'], message: '请输入名称' }],
          categoryCode: [{ required: true, trigger: ['blur', 'change'], message: '请输入编码' }],
        },
        title: '',
        dialogFormVisible: false,
        options: [],
      }
    },
    created() {},
    methods: {
      async fetchData() {
        await getList({ categoryId: this.form.categoryId }).then((res) => {
          if (res.success) {
            this.options = handleTree(res.data, 'categoryId', 'parentId', 'children', 0)
          }
        })
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.categoryId,
          label: node.categoryName,
          children: node.children,
        }
      },
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          if (row.parentId == 0) {
            row.parentId = undefined
          }
          this.form = Object.assign({}, row)
        }
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.categoryId) {
              await doEdit(this.form).then((res) => this.callbackFun(res))
            } else {
              await doAdd(this.form).then((res) => this.callbackFun(res))
            }
          } else {
            return false
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
